package org.syyo.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.syyo.admin.common.constant.PageConstant;
import org.syyo.admin.domain.ResultVo;
import org.syyo.admin.domain.request.NoticeReq;
import org.syyo.admin.domain.response.NoticeResp;
import org.syyo.admin.mapper.NoticeMapper;
import org.syyo.admin.service.NoticeService;
import org.syyo.admin.utils.DateUtils;
import org.syyo.admin.utils.ResultUtils;

import java.util.List;

/**
 * @Auther: wangzhong
 * @Date: 2019/12/4 18:08
 * @Description:
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private PageConstant pageConstant;

    @Override
    @Transactional
    public ResultVo addNotice(NoticeReq req) {
        req.setCreateDate(DateUtils.getDate());
        Integer integer = noticeMapper.addNotice(req);
        return ResultUtils.response(integer);
    }

    @Override
    @Transactional
    public ResultVo delNotice(NoticeReq req) {
        Integer integer = noticeMapper.delNotice(req);
        return ResultUtils.response(integer);
    }

    @Override
    @Transactional
    public ResultVo editNotice(NoticeReq req) {
        req.setUpdateDate(DateUtils.getDate());
        Integer integer = noticeMapper.editNotice(req);
        return ResultUtils.response(integer);
    }

    @Override
    public ResultVo getNotice(Integer noticeId) {
        NoticeResp notice = noticeMapper.getNotice(noticeId);
        return ResultUtils.response(notice);
    }

    @Override
    public ResultVo listNotice(NoticeReq req) {
        Integer pageNum = req.getPageNum();
        Integer pageSize = req.getPageSize();
        if(pageNum == null ){
            pageNum = pageConstant.getPageNum();
        }
        if(pageSize == null || pageSize > pageConstant.getPageRange() ){
            pageSize = pageConstant.getPageSize();
        }
        //分页处理
        PageHelper.startPage(pageNum,pageSize);
        List<NoticeResp> noticeResps = noticeMapper.listNotice(req);
        PageInfo<NoticeResp> list = new PageInfo<>(noticeResps);
        return ResultUtils.response(list);
    }
}
